<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // es5申明函数
    let fn = function fn() {
        console.log('es5');
    }

    // es6箭头函数
    // 1.没有this
    // 2.没有arguments
    let fn2 = (a, b) => {
        console.log('es6');
        return a + b;
    }
    const res = fn2(3, 5);
    console.log(res);

    // 1.箭头函数this是静态的, 始终指向函数声明时所在作用域下的this的值
    function getName() {
        console.log(this.name);
    }
    let getNames2 = () => {
        console.log(this.name);
    }
    window.name = "mortal";
    const school = {
        name: "mingquan"
    }
    // 直接调用都是指向windows变量
    getName();
    getNames2();

    // call方法调用,箭头函数不变还是mortal
    getName.call(school);
    getNames2.call(school);

    // 2.不能作为构造函数实例化对象
    let Person = (name, age) => {
        this.name = name;
        this.age = age;
    }
    // let me = new Person("mortal", 18);
    // console.log(me);
    // 报错：
    // Uncaught TypeError: Person is not a constructor

    // 3.不能使用arguments变量
    let func = () => {
        console.log(arguments);
    }
    // func(1, 2, 3);
    // 报错：
    // Uncaught ReferenceError: arguments is not defined

    // 4.简写
    // 省略小括号(当形参只有一个的时候)
    let add = n => {
        return n + n;
    }
    // 省略大括号(只有一行代码的时候)
    let multiply = n => n * n;
    const res1 = add(8);
    const res2 = multiply(8);
    console.log(res1, res2);


</script>

</html>